package pcb.views.library 
{
	import com.intel.utils.Debugger;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import pcb.views.uievents.SlideEvent;
	
	/**
	 * ...
	 * @author ...
	 */
	public class DragDrop extends MovieClip
	{
		private var iX:int;
		private var iY:int;
		
		private var mcDrag:MovieClip;
		private var mcDrop:MovieClip;
		
		//.5 sec delay on complete show me
		private var delay:Number = 500;

		
		public function DragDrop() 
		{
			mcDrag = this.getChildByName("mcDrag") as MovieClip;
			mcDrop = this.getChildByName("mcDrop") as MovieClip;
			
			if (mcDrag != null && mcDrop != null ) {			
				iX = mcDrag.x;
				iY = mcDrag.y;
				
				mcDrag.mouseChildren = false;
				mcDrag.buttonMode = true;
				mcDrag.useHandCursor = true;
				
				mcDrag.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
				mcDrag.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
				
			} else {				
				Debugger.traceOut("Error - could not locate mcDrag and/ or mcDrop movieclips", Debugger.ERROR);
				
			}
			dispatchEvent(new Event(SlideEvent.INTERACTIVITY_CREATED, true));
			this.addEventListener(Event.REMOVED, removed);
			
		}
		
		private function removeListeners():void 
		{
			if (mcDrag.hasEventListener(MouseEvent.MOUSE_DOWN)) {
				mcDrag.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			}
			
			if (mcDrag.hasEventListener(MouseEvent.MOUSE_UP)) {
				mcDrag.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
			}
		}
		
		private function mouseDown(e:MouseEvent):void {
			e.target.startDrag(true);
		}
		
		private function mouseUp(e:MouseEvent):void {
			e.target.stopDrag();
			if (e.target.hitTestObject(mcDrop)) {
				e.target.x = mcDrop.x;
				e.target.y = mcDrop.y;
				mcDrag.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown, false);
				mcDrag.removeEventListener(MouseEvent.MOUSE_UP, mouseUp, false);
				
				dispatchEvent(new Event(SlideEvent.INTERACTIVITY_COMPLETED, true));
				removeListeners();
			} else {
				reset();
			}
		}
		
		private function removed(e:Event):void 
		{
			removeListeners();
			this.removeEventListener(Event.REMOVED, removed);
		}
		
		
		private function reset():void {
			mcDrag.x = iX;
			mcDrag.y = iY;
		}
		
		public function addAnnotation(mcAnnotation:AnnotationMC):void 
		{
			mcAnnotation.name = "mcAnnotation";
			this.addChild(mcAnnotation);
		}
		
		public function getAnnotationMcPosition():DisplayObject {
			return this.mcDrag as DisplayObject;
		}
		
		public function showMe():void 
		{
			removeListeners();
			var sm:MovieClip = this.getChildByName("mcShowMe") as MovieClip;
			if (sm != null) {
				if (this.getChildByName("mcAnnotation")) {
					this.removeChild(this.getChildByName("mcAnnotation"));
				}
				mcDrag.visible = false;
				sm.visible = true;
				sm.play();
				sm.addEventListener(Event.ENTER_FRAME, showMeEnterFrame);
			} else {
				Debugger.traceOut("could not find show me MC", Debugger.ERROR);
			}
		}
		
		private function showMeEnterFrame(e:Event):void 
		{
			var mc:MovieClip = e.target as MovieClip;
			if (mc.currentFrame == mc.totalFrames) {
				var timer:Timer = new Timer(delay,1);
				timer.addEventListener(TimerEvent.TIMER_COMPLETE, t_onComplete);				
				timer.start();
				mc.removeEventListener(Event.ENTER_FRAME, showMeEnterFrame);
			}
			mc = null;
		}
		
		private function t_onComplete(e:TimerEvent):void 
		{
			e.target.removeEventListener(TimerEvent.TIMER_COMPLETE, t_onComplete);
			dispatchEvent(new Event(SlideEvent.INTERACTIVITY_COMPLETED, true));
		}
	}
	
}